package com.bei_en.teacher.admin.controller.learn;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.learn.bean.LearnReport;
import com.bei_en.teacher.admin.model.learn.condition.LearnReportCondition;
import com.bei_en.teacher.admin.model.learn.vo.LearnReportVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.service.learn.LearnReportService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.util.interceptor.OAuthRequired;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * 
 * @author Guocg
 * @date 2016-8-28 23:52:39
 */
@Controller
@RequestMapping("/api/LearnReport")
public class LearnReportAPPController extends BaseController {

	@Autowired
	LearnReportService learnReportService;

	@RequestMapping("/learnReportList")
	@ResponseBody
	@OAuthRequired
	public JsonResult learnReportList(Model model,
			@Valid LearnReportCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			UsersVo usersVo = getUser(session);
			List<LearnReportVo> list = new ArrayList<LearnReportVo>();
			vo.setUserId(usersVo.getUserId());
			list = learnReportService.findLearnReportPage(vo);
			JSONArray array = new JSONArray();
			JSONObject object = null;
			LearnReportVo learnReportVo = null;
			for (int i = 0; i < list.size(); i++) {
				object = new JSONObject();
				learnReportVo = list.get(i);
				object.put("Id", learnReportVo.getId());
				object.put("title", learnReportVo.getTitle());
				object.put("answerRate", learnReportVo.getAnswerRate());
				object.put("createTime", learnReportVo.getCreateTime());
				array.add(object);
			}
			return new JsonSuccessResult(session.getId(), array);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}
	@RequestMapping("/cancellearn")
	@ResponseBody
	public JsonResult cancellearn(Model model, @Valid LearnReportCondition vo,
			BindingResult br, HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
				learnReportService.delete(vo.getId());
			return new JsonSuccessResult(session.getId(), "删除成功","");

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}
	@RequestMapping("/learnReport")
	@ResponseBody
	@OAuthRequired
	public JsonResult learnReport(Model model, @Valid LearnReportCondition vo,
			BindingResult br, HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			UsersVo usersVo = getUser(session);
			List<LearnReportVo> list = new ArrayList<LearnReportVo>();
			vo.setUserId(usersVo.getUserId());
			list = learnReportService.findLearnReportPage(vo);
			JSONArray array = new JSONArray();
			JSONObject object = null;
			LearnReportVo learnReportVo = null;
			for (int i = 0; i < list.size(); i++) {
				object = new JSONObject();
				learnReportVo = list.get(i);
				object.put("Id", learnReportVo.getId());
				object.put("questionsTypeId",
						learnReportVo.getQuestionsTypeId());
				object.put("userId", learnReportVo.getUserId());
				object.put("type", learnReportVo.getType());
				object.put("title", learnReportVo.getTitle());
				object.put("answerRate", learnReportVo.getAnswerRate());
				object.put("yesFraction", learnReportVo.getYesFraction());
				object.put("totalFraction", learnReportVo.getTotalFraction());
				object.put("averageFraction",
						learnReportVo.getAverageFraction());
				object.put("averageTime", learnReportVo.getAverageTime());
				object.put("countryTime", learnReportVo.getCountryTime());
				object.put("createTime", learnReportVo.getCreateTime());
				array.add(object);
			}
			return new JsonSuccessResult(session.getId(), array);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/learnReportAdd")
	@ResponseBody
	public JsonResult learnReportAdd(Model model,
			@Valid LearnReportCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {

			LearnReportVo learnReport = new LearnReportVo();
			learnReportService.insert(learnReport);
			return new JsonSuccessResult(session.getId(), "成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/learnReportEdit")
	public JsonResult learnReportEdit(Model model,
			@Valid LearnReportCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			LearnReportVo learnReport = new LearnReportVo();

			learnReportService.updateSelective(learnReport);

			return new JsonSuccessResult(session.getId(), "成功");

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

}
